/*
 * Copyright 2019-2029 geekidea(https://github.com/geekidea)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ld.security.auth.shiro.util;

import com.ld.security.auth.common.sys.login.LoginService;
import com.ld.security.auth.common.sys.login.UserService;
import com.ld.security.auth.common.sys.po.LoginPo;
import com.ld.security.auth.common.sys.vo.UserDetailsVo;
import com.ld.security.auth.common.sys.vo.UserInfoVo;
import com.ld.security.auth.shiro.login.jwt.JwtProperties;
import com.ld.security.auth.shiro.login.jwt.JwtToken;
import com.ld.security.auth.shiro.login.pass.PasswordToken;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.tomcat.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 获取登陆信息工具类
 *
 * @author geekidea
 * @date 2018-11-08
 */
@Slf4j
@Component
public class LoginUtil {

    private static JwtProperties jwtProperties;


    private static UserService userService;


    private static List<LoginService> loginServiceList;
    /**
     * 获取当前登陆用户的ID
     *
     * @return
     */
    public static String getUserId() {
        return getUserInfo().getUser().getId();
    }

    /**
     * 获取当前登陆用户的基本年数据
     *
     * @return
     */
    public static UserInfoVo getUserInfo() {
        String username =getUsername();
        return userService.getUserInfo(username);
    }


    /**
     * 获取当前登陆用户的详细数据
     *
     * @return
     */
    public static UserDetailsVo getUserDetails() {

       String username=getUsername();
        return userService.getUserDetails(username);
    }

    /**
     * 获取当前登陆用户的账号
     *
     * @return
     */
    public static String getUsername()
    {

        login();
        Object val =  SecurityUtils.getSubject().getPrincipal();
        String username=null;
        if(val instanceof JwtToken){
            JwtToken token = (JwtToken)val;
            username=token.getUsername();
        }else if(val instanceof PasswordToken){
            PasswordToken token = (PasswordToken)val;
            username = token.getUsername();
        }else{
            username =val.toString();
        }

        return username;
    }


    @Autowired
    public  void setUserService(UserService userService) {
        LoginUtil.userService = userService;
    }

    /**
     * 根据token登录
     */
    public static void login() {

    for (LoginService loginService : loginServiceList) {
      if(loginService.isUse()){
          loginService.login();
          break;
      }
    }

    }


    public static void logout() {
        for (LoginService loginService : loginServiceList) {
            if(loginService.isUse()){
                 loginService.logout();
                break;
            }
        }
    }

        public static String login(LoginPo po) {

        for (LoginService loginService : loginServiceList) {
            if(loginService.isUse()){
                return loginService.login(po);
            }
        }
        return null;

    }

    public static String getToken(){
        String tokenName = jwtProperties.getTokenName();
        String token = SessionUtil.getHeader(tokenName);
        if(StringUtil.isNull(token)){
            token = SessionUtil.getParameter(tokenName);
        }
        if(StringUtil.isNull(token)){
            token = SessionUtil.getCookie(tokenName);
        }
        return token;
    }
    public static boolean isAdmin(){
        return true;
    }

    public static boolean isLogin(){
        return !SecurityUtils.getSubject().isAuthenticated();
    }

    @Autowired
    public void setJwtProperties(JwtProperties jwtProperties) {
        LoginUtil.jwtProperties = jwtProperties;
    }



    @Autowired
    public  void setLoginServiceList(List<LoginService> loginServiceList) {
        LoginUtil.loginServiceList = loginServiceList;
    }
}
